'use strict';
import '../css/index.css'
import Nanobar from 'nanobar'
import axios from 'axios'
import OSS from 'ali-oss'

let nanobar = new Nanobar();
nanobar.go(100);

async function upLoadFile() {
    try {
        let data = await axios.get('http://192.168.1.77:3000/token')
        console.log(data)
    } catch (e) {

    }
}

// upLoadFile()
$("#file").change(async (e) => {
    let files = e.target.files[0]
    if(!files) return false
    let {name, type, size} = files
    // let formData = new FormData()
    let client = await getToken()
    let options = {progress}
    // let data = await client.multipartUpload(name, files, options)
    let data = await client.put(name, files)
    let img = document.createElement('img')
    img.src=data.url

    document.querySelector('body').appendChild(img)
    // console.log(files)
    console.log(data)
    console.log(img)


})

// upLoadFile()
$("#file1").change(async (e) => {
    let files = e.target.files[0]
    if(!files) return false
    let {name, type, size} = files
    let client = await getToken()
    let options = {progress}
    let data = await client.multipartUpload(name, files, options)
    let url = client.getObjectUrl(name)
    let img = document.createElement('img')
    img.src=url
    document.querySelector('body').appendChild(img)
    console.log(data)
    console.log(url)

})


const appServer = 'http://192.168.1.77:3000/token';
const urllib = OSS.urllib;
let region = 'oss-cn-beijing';
let bucket = 'w-static';

/*
*
* @param  percentage {Number} 进度百分比(0-1之间小数)。
* @param  checkpoint {Object} 断点记录点。
* @param  res {Object}) 单次part成功返回的response。
* */

async function progress(percentage, checkpoint, res) {
    console.log('=========================================')
    console.log(percentage)
    console.log(checkpoint)
    console.log(res)
}

async function getToken() {
    let token = await urllib.request(appServer, {
        method: 'GET'
    });
    token = JSON.parse(token.data);
    const client = new OSS({
        region: region,
        accessKeyId: token.AccessKeyId,
        accessKeySecret: token.AccessKeySecret,
        stsToken: token.SecurityToken,
        bucket: bucket
    });
    return client
    /*
    * @param   name {String} Object名称。
    * @param   file  {File} HTML5网络文件或file {Blob} Blob的数据格式。
    * @param   option {Object}
    * */

    // client.multipartUpload(na)
    // client.multipartUpload(name, file, options).then((res) => {
    //     console.log('upload success: %j', res);
    //     currentCheckpoint = null;
    //     uploadFileClient = null;
    // }).catch((err) => {
    //     if (uploadFileClient && uploadFileClient.isCancel()) {
    //         console.log('stop-upload!');
    //     } else {
    //         console.error(err);
    //     }
    // });


    // console.log(token)
}

getToken()

// let currentCheckpoint;
// const progress = async function progress(p, checkpoint) {
//     currentCheckpoint = checkpoint;
//     const bar = document.getElementById('progress-bar');
//     bar.style.width = `${Math.floor(p * 100)}%`;
//     bar.innerHTML = `${Math.floor(p * 100)}%`;
// };
//
// let uploadFileClient;
// const uploadFile = function (client) {
//     if (!uploadFileClient || Object.keys(uploadFileClient).length === 0) {
//         uploadFileClient = client;
//     }
//
//     const file = document.getElementById('file').files[0];
//     const key = document.getElementById('object-key-file').value.trim() || 'object';
//     console.log(`${file.name} => ${key}`);
//
//     const options = {
//         progress,
//         partSize: 100 * 1024,
//         meta: {
//             year: 2017,
//             people: 'test',
//         },
//     };
//
//     return client.multipartUpload(key, file, options).then((res) => {
//         console.log('upload success: %j', res);
//         currentCheckpoint = null;
//         uploadFileClient = null;
//     }).catch((err) => {
//         if (uploadFileClient && uploadFileClient.isCancel()) {
//             console.log('stop-upload!');
//         } else {
//             console.error(err);
//         }
//     });
// };
// applyTokenDo(uploadFile);
async function getList(){
    let client = await getToken()
    let result = await client.list();
    console.log(result)
}
// let res = getList()
